package p_2023_03_09_StudentSystem;

import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

public class App {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        ArrayList<User> list = new ArrayList<>();

        while (true) {
            menu();
            System.out.println("请输入您的选择：");
            String select = sc.next();
            switch (select) {
                case "1" -> login(list);
                case "2" -> register(list);
                case "3" -> forgetPassword(list);
                case "4" -> printUser(list);
                case "0" -> {
                    System.out.println("欢迎再次使用！");
                    System.exit(0);
                }
                default -> System.out.println("没有这个选项！请重新选择！");
            }
        }

    }

    public static void menu() {
        System.out.println("=======欢迎来到学生管理系统======");
        System.out.println("1. 登录");
        System.out.println("2. 注册");
        System.out.println("3. 忘记密码");
        System.out.println("4. 打印所有用户信息");
        System.out.println("0. 退出");
        System.out.println("===============================");
    }

    private static void register(ArrayList<User> list) {
        Scanner sc = new Scanner(System.in);
        String userName;
        //1.用户名验证
        while (true) {
            System.out.println("请输入用户名：（只能是字母加数字组合， 但是不能是纯数字）");
            userName = sc.next();
            boolean flag1 = checkUserName(list, userName);
            if (flag1) {
                System.out.println("用户名验证成功！");
                break;
            } else {
                System.out.println("用户名不合法，请重新输入！");
            }
        }
        //2. 密码键盘输入两次，两次一致才可以注册
        String password;
        while (true) {
            System.out.println("请输入密码：");
            password = sc.next();
            System.out.println("请确认密码：");
            String againPassword = sc.next();
            if (password.equals(againPassword)) {
                System.out.println("密码验证成功！");
                break;
            } else {
                System.out.println("两次密码不一致，请重新输入！");
            }
        }
        String personId;
        //3. 身份证号码验证
        while (true) {
            System.out.println("请输入身份证号码：");
            personId = sc.next();
            boolean flag2 = checkPersonId(personId);
            if (flag2) {
                System.out.println("身份验证成功！");
                break;
            } else {
                System.out.println("身份证号码不正确，请重新输入！");
            }
        }
        //4.手机号验证
        String phoneNumber;
        while (true) {
            System.out.println("请输入手机号：");
            phoneNumber = sc.next();
            boolean flag3 = checkPhoneNumber(phoneNumber);
            if (flag3) {
                System.out.println("手机号验证成功！");
                break;
            } else {
                System.out.println("手机号格式不正确，请检查后重新输入！");
            }
        }

        User user = new User(userName, password, personId, phoneNumber);
        list.add(user);
        System.out.println("注册成功！");
    }

    private static void printUser(ArrayList<User> list) {
        if (list.size() == 0) {
            System.out.println("There is Empty!\n");
            return;
        }
        System.out.println("=============用户信息如下==========");
        for (int i = 0; i < list.size(); i++) {
            User u = list.get(i);
            System.out.println(u.getUserName() + ", " + u.getPassword() + ", " + u.getPersonID() + ", " + u.getPhoneNumber());
        }
        System.out.println("==================================\n");
    }

    private static boolean checkPhoneNumber(String phoneNumber) {
        //1.长度为 11 位
        if (phoneNumber.length() != 11) {
            return false;
        }
        //2. 不能以 0 开头
        if (phoneNumber.startsWith("0")) {
            return false;
        }
        //3.全部是数字
        for (int i = 0; i < phoneNumber.length(); i++) {
            char c = phoneNumber.charAt(i);
            if (!(c >= '0' && c <= '9')) {
                return false;
            }
        }
        return true;
    }

    private static boolean checkPersonId(String personId) {
        //1.长度为 18 位
        if (personId.length() != 18) {
            System.out.println("长度不是18位！");
            return false;
        }
        //2.不能以 0 开头
        if (personId.startsWith("0")) {
            System.out.println("不能以0开头");
            return false;
        }
        //3.前 17 位必须是数字
        for (int i = 0; i < personId.length() - 1; i++) {
            char c = personId.charAt(i);
            if (!(c >= '0' && c <= '9')) {
                System.out.println("前17位不全是数字");
                return false;
            }
        }
        char c = personId.charAt(personId.length() - 1);
        if ((c >= '0' && c <= '9') || (c == 'x') || (c == 'X')) {
            return true;
        } else {
            return false;
        }
    }

    private static boolean checkUserName(ArrayList<User> list, String userName) {
        //2.用户名长度必须在 3~15 之间
        int size = userName.length();
        if (size < 3 || size > 15) {
            return false;
        }

        //3.只能是字母加数字组合， 但是不能是纯数字
        for (int i = 0; i < userName.length(); i++) {
            char c = userName.charAt(i);
            if (!((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9'))) {
                return false;
            }
        }

        int count = 0;
        for (int i = 0; i < userName.length(); i++) {
            char c = userName.charAt(i);
            if ((c > 'a' && c < 'z') || (c > 'A' && c < 'Z')) {
                count++;
                break;
            }
        }
        if (count == 0) {
            return false;
        }
        //1.用户名唯一
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getUserName().equals(userName)) {
                return false;
            }
        }
        return true;
    }

    private static void login(ArrayList<User> list) {
        Scanner sc = new Scanner(System.in);
        for (int i = 0; i < 3; i++) {
            System.out.println("请输入用户名：");
            String username = sc.next();
            boolean flag = contains(list, username);
            if (!flag) {
                System.out.println("用户" + username + "未注册, 清先注册！");
                return;
            }

            //2. 输入密码
            System.out.println("请输入密码：");
            String password = sc.next();
            //3. 生成验证码
            while (true) {
                String code = getCode();
                System.out.println(code);
                System.out.println("请输入验证");
                String uCode = sc.next();
                if (code.equalsIgnoreCase(uCode)) {
                    break;
                } else {
                    System.out.println("验证码不正确，请重新输入！");
                }
            }
            //4.判断账号密码是否正确
            User userInfo;
            userInfo = new User(username, password, null, null);
            boolean result = checkUserInfo(list, userInfo);
            if (result) {
                System.out.println("登录成功！");
                StudentSystem ss = new StudentSystem();
                ss.startStudentSystem();
                break;
            } else {
                if (i == 2) {
                    System.out.println("用户" + username + "已锁定，请联系工作人员！");
                    return;
                }
                System.out.println("账号或密码不正确,请重新输入:( 您还有" + (2 - i) + "次机会！)");
            }
        }
    }

    private static String getCode() {
        ArrayList<Character> list = new ArrayList<>();
        for (int i = 0; i < 26; i++) {
            list.add((char) ('a' + i));
            list.add((char) ('A' + i));
        }
        //随机抽取 4 个字符
        Random r = new Random();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < 4; i++) {
            int index = r.nextInt(list.size());
            char c = list.get(index);
            sb.append(c);
        }
        //随机生成一个数字
        int num = r.nextInt(10);
        sb.append(num);
        char[] arr = sb.toString().toCharArray();
        int rIndex = r.nextInt(arr.length);
        char temp = arr[rIndex];
        arr[rIndex] = arr[arr.length - 1];
        arr[arr.length - 1] = temp;
        String result = new String(arr);
        return result;

    }

    private static boolean checkUserInfo(ArrayList<User> list, User user) {
        for (int i = 0; i < list.size(); i++) {
            User u = list.get(i);
            if (u.getUserName().equals(user.getUserName()) && u.getPassword().equals(user.getPassword())) {
                return true;
            }
        }
        return false;
    }

    private static boolean contains(ArrayList<User> list, String username) {
        for (int i = 0; i < list.size(); i++) {
            User u = list.get(i);
            if (u.getUserName().equals(username)) {
                return true;
            }
        }
        return false;
    }

    private static void forgetPassword(ArrayList<User> list) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入用户名：");
        String userName = sc.next();
        boolean flag = contains(list, userName);
        if (!flag) {
            System.out.println("用户：" + userName + "不存在！");
            return;
        }

        System.out.println("请输入身份证号码：");
        String personId = sc.next();
        System.out.println("请输入手机号：");
        String phoneNumber = sc.next();

        int index = getInedx(list, userName);
        User user = list.get(index);
        if (!(user.getPersonID().equalsIgnoreCase(personId) && user.getPhoneNumber().equals(phoneNumber))) {
            System.out.println("身份账号码或手机号不一致，请重新输入！");
            return;
        }

        while (true) {
            System.out.println("请输入新密码：");
            String newPassword = sc.next();
            System.out.println("请再次输入新密码：");
            String againPassword = sc.next();

            if (newPassword.equals(againPassword)) {
                user.setPassword(newPassword);
                System.out.println("修改成功！");
                break;
            } else {
                System.out.println("两次新密码不一致，请重新输入！");
            }
        }

    }

    private static int getInedx(ArrayList<User> list, String userName) {
        for (int i = 0; i < list.size(); i++) {
            User user = list.get(i);
            if (user.getUserName().equals(userName)) {
                return i;
            }
        }
        return -1;
    }

}
